package com.cqu.hhx.driver.service.impl;

import com.cqu.hhx.driver.entity.*;
import com.cqu.hhx.driver.entity.forWeb.ChangeReq;
import com.cqu.hhx.driver.mapper.*;
import com.cqu.hhx.driver.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

/**
 * @author DJ
 * @create 2020-05-26 21:58
 */
@Service
public class ManageServiceImpl implements ManageService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private FinishOrderMapper finishOrderMapper;
    @Autowired
    private DoingOrderMapper doingOrderMapper;
    @Autowired
    private NewOrderMapper newOrderMapper;

    /**
     * 删除司机
     * @param driverId
     * @return
     */
    @Override
    public Integer deleteDriver(Long driverId) {
        try{
            driverMapper.deleteDriver(driverId);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 更新司机信息
     * @param driverId
     * @param changeReq
     * @return
     */
    @Override
    public Integer updateDriver(Long driverId, ChangeReq changeReq) {
        try{
            driverMapper.updateDriver(driverId,changeReq.getName(),changeReq.getPhone());
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 重置司机密码
     * @param driverId
     * @return
     */
    @Override
    public Integer resetDriverPassword(Long driverId) {
        try{
            driverMapper.resetPassword(driverId);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 通过电话号码获取司机信息
     * @param phone
     * @return
     */
    @Override
    public Driver getDriverInfoByPhone(String phone) {
        try{
           Driver driver=driverMapper.getDriverInfo(phone);
           return driver;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 重置乘客密码
     * @param customerId
     * @return
     */
    @Override
    public Integer resetCustomerPassword(Long customerId) {
        try{
            customerMapper.resetPassword(customerId);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 删除乘客
     * @param customerId
     * @return
     */
    @Override
    public Integer deleteCustomer(Long customerId) {
        try{
            customerMapper.deleteCustomer(customerId);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 更新乘客信息
     * @param customerId
     * @param changeReq
     * @return
     */
    @Override
    public Integer updateCustomer(Long customerId, ChangeReq changeReq) {
        try{
            customerMapper.updateCustomer(customerId,changeReq.getName(),changeReq.getPhone());
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 通过手机号获取乘客信息
     * @param phone
     * @return
     */
    @Override
    public Customer getCustomerInfoByPhone(String phone) {
        try{
            Customer customer=customerMapper.getCustomerInfo(phone);
            return customer;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过乘客Id获取乘客信息
     * @param customerId
     * @return
     */
    @Override
    public Customer getCustomerInfoById(Long customerId) {
        try {
            Customer customer=customerMapper.getCustomerInfoById(customerId);
            return customer;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过日期查询获取订单信息
     * @param selectedDay
     * @param customerId
     * @return
     */
    @Override
    public List<FinishOrder> getCustomerFinishedOrder(Date selectedDay, Long customerId) {
        try {
            List<FinishOrder> orders=finishOrderMapper.getCustomerFinishedOrder(selectedDay,customerId);
            return orders;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过日期查询获取订单信息
     * @param selectedDay
     * @param driverId
     * @return
     */
    @Override
    public List<FinishOrder> getDriverFinishedOrder(Date selectedDay, Long driverId) {
        try {
            List<FinishOrder> orders=finishOrderMapper.getDriverFinishedOrder(selectedDay,driverId);
            return orders;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取选择日期的所有已完成订单
     * @param selectedDay
     * @return
     */
    @Override
    public List<FinishOrder> getAllFinishOrderByDate(Date selectedDay) {
        try {
            List<FinishOrder> orders=finishOrderMapper.getAllFinishedOrder(selectedDay);
            return orders;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前所有进行中的订单
     * @return
     */
    @Override
    public List<DoingOrder> getAllDoingOrder() {
        try {
            List<DoingOrder> orders=doingOrderMapper.getAllDoingOrder();
            return orders;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前所有新建订单
     * @return
     */
    @Override
    public List<NewOrder> getAllNewOrder() {
        try {
            List<NewOrder> orders=newOrderMapper.getAllNewOrder();
            return orders;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
